<?php
class InfoModel extends Model {
	private $msgError;
	protected $_validate = array(
	    array('title','require','请填写标题！'),
    );
	protected $_auto = array(
        array('datetime','time',1,'function'),
    );
	
	//更新点击量
	function update_click($id,$mid=0){
		$info = $this->where(array('id' => $id))->find();
		if($info['member_id'] != $mid){
			$updata['click'] = $info['click'] + 1;
			$this->where(array('id' =>$id))->data($updata)->save();
		}
	}
	public function getList($listname,$view,$map){
		$list=$this->where($map)->order('datetime')->limit(6)->select();
		$view->assign($listname,$list);
	}
	public function _list($view,$map,$sort='sort',$pagesize=20){
		//计算总页数
		$count=$this->where($map) ->count();
		//实例化分页类
		import ( 'ORG.Util.Page' );
		$p=new Page($count,$pagesize);
		$list = $this->where($map)->order($sort)->limit($p->firstRow.',
'.$p->listRows)->select();
        
        $pages=$p->show();
		$view->assign('list',$list);
		$view->assign('pages',$pages);
	}
	
	//发送信息的方法
	public function sendMsg($data){
		$config=C('MSG_CONFIG');
		$memberDB=M('Member');
		$i=0;
		
		//循环引入类库
		import("@.ORG.Sms.Msg");
		$mode=explode(',',C('MSG_TYPE'));
		foreach($mode as $class){
			import("@.ORG.Sms.".$class);
		}
		
		//循环传过来的用户
		foreach($data as $user){
			//保存信息
			$user['site_id']=__SITEID__;
			$user['datetime']=time();
			$ret=$this->add($user);
			if(!$ret){return false;}
			
			//获取发送对象和发送模式
			$touser=$memberDB->where('id='.$user['tomemberid'])->Field('id as member_id,openid,tel,username')->find();
			$mode=array_intersect(explode(',',$user['mode']),$mode);
			if(empty($user['mode'])&&!$touser){continue;}
			
			
			//循环发送模式 发送信息
			foreach($mode as $vo){
				$class=new $vo($touser[$config[$vo]['touser']],$user['content'],session ( 'loginUserName'));
				$r=$class->sendMsg();
				if($r){
					$i++;	
				}else{
					$this->msgError[]=$r;	
				}
				
			}
		}
		//返回发送成功的数量
		return $i;
	}
	
		
		
	//筛选数据
	public function filter($type,$view){
		if($type!='notice'&&$type!='plan'){
			//筛选可查看的指定用户
			$where['r.member_id']=$_SESSION[C('USER_AUTH_KEY')];
			$where['r.pid']=array('like','%'.$_SESSION[C('USER_AUTH_KEY')].'%');
			$where['_logic']='or';
			$map['_complex']=$where;
			//筛选某个职员 或一整个部门
			if(""!=trim($_POST['member_id'])){
				$map['r.member_id']=$_POST['member_id'];    
			}else{
				if(""!=trim($_POST['groupid'])){
					$groupid=D("Member_group")->getGroupField($_POST['groupid']);
					$map['m.group_id']=array('in',$groupid); 
				}  
			}
		}
        //筛选开始时间与结束时间
        if(trim($_POST['starttime'])!=''){
            $map['r.datetime']=array('GT',strtotime($_POST['starttime']));
        }
        if(trim($_POST['endtime'])!=''){
            $map['r.datetime']=array('LT',strtotime($_POST['endtime']));
        }
        if(""==trim($_POST['starttime'])&&""==trim($_POST['endtime'])){
            //$map['r.datetime']=array('GT',strtotime("-1 month"));
        }
        if(""!=trim($_POST['starttime'])&&""!=trim($_POST['endtime'])){
            $map['r.datetime']=array('between',array(strtotime($_POST['starttime']),strtotime($_POST['endtime'])));
        }
        //筛选状态
        if(""!=trim($_POST['statu_id'])){
            $map['r.status']=$_POST['statu_id'];    
        }
		//筛选提醒类型
		if(isset($_POST['level'])){
			$map['r.level']=$_POST['level'];	
		}
		
        $map['r.site_id']=__SITEID__;
        $map['r.type']=$type;
        //获取筛选后的积分事件列表
		if($type=='remind'||$type=='log'||$type=='share'||$type=='notice'){
			$list = $this->alias('r')->join('left join __MEMBER__ m on r.member_id=m.id')->where($map)->Field('r.*,m.name')->select();
		}else if($type=='0'){
			$list = $this->alias('r')->where($map)->Field('r.*')->select();
		}else if($type=='approval'){
			$list = $this->alias('r')->join('left join __MEMBER__ m on r.member_id=m.id')->join('left join __MEMBER__ m2 on r.pid=m2.id')->where($map)->Field('r.*,m.name,m2.name as checkname')->select();	
		}else if($type=='plan'){
			if(""!=trim($_POST['member_id'])){
				$map['r.member_id']=$_POST['member_id'];    
			}else{
				if(""!=trim($_POST['groupid'])){
					$map['g2.id']=$_POST['groupid'];
				}else{
					if(trim($_POST['getall'])=='one'){
						$getall=array('全部员工','all');
						$map['r.member_id']=session(C('USER_AUTH_KEY')); 
					}else{
						$map['g2.pid']=0;  
						$getall=array('查看自己','one');
					}
				}
			}
			$getall=isset($getall)?$getall:array('查看自己','one');
	    	$view->assign('getall',$getall);
			
			
			//获取筛选后的列表
			$list = $this->alias('r')->join('left join __MEMBER__ m on r.member_id=m.id')->join('left join __MEMBER_GROUP__ g on g.id=m.group_id')->join('left join __MEMBER_GROUP__ g2 on g2.id=g.pid')->where($map)->order('id desc')->Field('r.*,g2.title as groupname,m.name')->select();
		}
        $view->assign('list',$list);
	}
    public function getinfo($id){
        return $this->where(array('id'=>$id))->find();    
    }
}